import { Component, OnInit, HostBinding, Input } from '@angular/core';

@Component({
    selector: 'guide-sample-reddit-article',
    templateUrl: './sample-reddit-article.component.html',
    styleUrls: ['./sample-reddit-article.component.css']
})
export class SampleRedditArticleComponent implements OnInit {
    @Input() article: Article;
    @HostBinding() class = 'row';
    constructor() { }

    ngOnInit() {
    }
    voteUp($event): boolean {
        this.article.voteUp();
        return false;
    }
    voteDown(): boolean {
        this.article.voteDown();
        return false;
    }
}

export class Article {
    title: string; link: string; votes: number;
    voteUp(): void {
        this.votes += 1;
    }
    voteDown(): void {
        this.votes -= 1;
    }
    domain(): string {
        try {
            const link: string = this.link.split('//')[1];
            return link.split('/')[0];
        } catch (err) {
            return null;
        }
    }
    constructor(title: string, link: string, votes?: number) {
        this.title = title;
        this.link = link;
        this.votes = votes || 0;
    }
}
